package Dialog;

import java.awt.Checkbox;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import utils.ImageUtils;
import utils.Parameters;
import view.MainFrame;


public class NewFileDialog extends JDialog
{
    private JLabel     m_titleLabel;
    private JLabel     m_widthLabel;
    private JLabel     m_heightLabel;
    private JLabel     m_colorLabel;
    private JLabel     m_opacityLabel;

    private JTextField m_titleField;
    private JSpinner   m_widthSpinner;
    private JSpinner   m_heightSpinner;
    private JTextField m_colorField;
    private JSlider    m_sliderOpacity;

    private JButton    m_confirmButton;
    private JButton    m_cancelButton;

    public NewFileDialog()
    {
        super(MainFrame.getInstance(), "Create a new file.");

        initializeFrame();
        initializeField();

        this.setVisible(true);
    }

    public void initializeFrame()
    {
        this.setSize(Parameters.NF_WIDTH, Parameters.NF_HEIGHT);
        this.getContentPane().setLayout(new GridLayout(6, 2));
        this.setLocationRelativeTo(MainFrame.getInstance());
    }

    public void initializeField()
    {
        m_titleLabel = new JLabel("Title: ");
        m_widthLabel = new JLabel("Width: ");
        m_heightLabel = new JLabel("Height: ");
        m_colorLabel = new JLabel("Background: ");
        m_opacityLabel = new JLabel("Opacity: (100%) ");

        m_titleField = new JTextField("Untitled");
        m_widthSpinner = new JSpinner(new SpinnerNumberModel(Parameters.DOCUMENT_DEFAULT_WIDTH, 1, 3000, 10));
        m_heightSpinner = new JSpinner(new SpinnerNumberModel(Parameters.DOCUMENT_DEFAULT_HEIGHT, 1, 3000, 10));
        m_colorField = new JTextField();
        m_colorField.setEditable(false);
        m_colorField.setBackground(Color.WHITE);
        m_sliderOpacity = new JSlider(JSlider.HORIZONTAL, 0, 255, 255);

        m_confirmButton = new JButton("Create");
        m_cancelButton = new JButton("Cancel");

        m_colorField.addMouseListener(new MouseListener()
        {

            @Override
            public void mouseReleased(MouseEvent arg0)
            {

            }

            @Override
            public void mousePressed(MouseEvent arg0)
            {
                m_colorField.setBackground(JColorChooser.showDialog(null,
                        "Choose background colour", Color.white));
            }

            @Override
            public void mouseExited(MouseEvent arg0)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent arg0)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent arg0)
            {
                // TODO Auto-generated method stub

            }
        });
        m_sliderOpacity.addChangeListener(new ChangeListener()
        {
            @Override
            public void stateChanged(ChangeEvent arg0)
            {
            	
                m_opacityLabel.setText("Opacity: ("+ImageUtils.alphaToInt(m_sliderOpacity.getValue())+"%)");
            }
        });

        m_cancelButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                dispose();
            }
        });
        m_confirmButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                MainFrame.getInstance().createNewFile(m_titleField.getText(),
                        (Integer) m_widthSpinner.getValue(),
                        (Integer) m_heightSpinner.getValue(),
                        m_colorField.getBackground(),
                        m_sliderOpacity.getValue());
                dispose();

            }
        });
        
        
        this.getContentPane().add(m_titleLabel);
        this.getContentPane().add(m_titleField);
        this.getContentPane().add(m_widthLabel);
        this.getContentPane().add(m_widthSpinner);
        this.getContentPane().add(m_heightLabel);
        this.getContentPane().add(m_heightSpinner);
        this.getContentPane().add(m_colorLabel);
        this.getContentPane().add(m_colorField);
        this.getContentPane().add(m_opacityLabel);
        this.getContentPane().add(m_sliderOpacity);
        this.getContentPane().add(m_confirmButton);
        this.getContentPane().add(m_cancelButton);
    }
}
